package com.zengqingfa.demo.controller;

import com.zengqingfa.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zengqingfa
 * @date 2019/11/12 16:36
 * @description
 * @email zengqingfa_java@163.com
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    //处理文件上传
    @RequestMapping(value = "/testuploadimg", method = RequestMethod.POST)
    public String uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String info = null;
        try {
            info = FileUtil.uploadFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    //处理多文件上传
    @RequestMapping(value = "/testuploadimgs", method = RequestMethod.POST)
    public String multipleFilesUpload(HttpServletRequest request) {
        //获取上传的文件数组
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        //遍历处理文件
        String info = null;
        for (MultipartFile file : files) {
            try {
                String s = FileUtil.uploadFile(file);
                info = info + "-" + s;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return info;
    }


}
